﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Text;
using InfoSoftGlobal;

public partial class bargraph_release : System.Web.UI.Page
{
    SQLInteract objSQL = new SQLInteract();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillSystemdropdown();
        }
    }

    public void fillSystemdropdown()
    {        
        DataSet dsSystem = new DataSet();
        try{
            dsSystem=objSQL.ReturnDataAdapter("select distinct System_Detected_In from Defects order by System_Detected_In desc");
            ddSystem.DataSource = dsSystem;
            ddSystem.DataTextField = "System_Detected_In";
            ddSystem.DataValueField = "System_Detected_In";
            ddSystem.DataBind();
        }
        catch (Exception ex){
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"Error: " + ex.Message + "\");</script>", false);
        }
        finally{
        }
    }


    protected void ddSystem_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddRelease.Items.Clear();
        ListItem li = new ListItem("Choose");
        li.Value = "-1";
        ddRelease.Items.Add(li);
        divProject.Visible = false;
        btnChart.Visible = false;
        fcLiteral.Visible = false;
        fillReleasedropdown();
    }

    public void fillReleasedropdown()
    {
        DataSet dsRelease = new DataSet();
        try
        {
            dsRelease=objSQL.ReturnDataAdapter("select distinct Detected_In_Release from Defects where System_Detected_In='" + ddSystem.SelectedValue.ToString().Trim() + "' order by Detected_In_Release desc");
            ddRelease.DataSource = dsRelease;
            ddRelease.DataTextField = "Detected_In_Release";
            ddRelease.DataValueField = "Detected_In_Release";
            ddRelease.DataBind();
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"Error: " + ex.Message + "\");</script>", false);
        }
        finally
        {
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        fcLiteral.Visible = false;
        string Release_name = ddRelease.SelectedValue.ToString().Trim();
        DataSet dsProject = new DataSet();
        try
        {
            dsProject=objSQL.ReturnDataAdapter("select distinct(Project) from Defects where System_Detected_In='" + ddSystem.SelectedValue.ToString().Trim() + "' and Detected_In_Release='" + Release_name + "' order by Project");
            if (dsProject.Tables[0].Rows.Count > 0)
            {
                cbProject.DataSource = dsProject;
                cbProject.DataTextField = "Project";
                cbProject.DataValueField = "Project";
                cbProject.DataBind();
                divProject.Visible = true;
                btnChart.Visible = true;
            }
            else
            {
                divProject.Visible = false;
                this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"" + "No Project found for selected release." + "\");</script>", false);
            }
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"Error: " + ex.Message + "\");</script>", false);
            divProject.Visible = false;
        }
        finally
        {

        }
    }

    protected void btnChart_Click(object sender, EventArgs e)
    {
        Table DynTable = new Table();
        DynTable.ID = "lnkchart";
        DynTable.BorderWidth = 0;
        DynTable.CellPadding = 0;
        DynTable.CellPadding = 0;
        string project_list = hdnProject.Value.ToString().Trim().Replace("amp;", "");
        string release_name = ddRelease.SelectedValue.ToString().Trim();
        string system_name = ddSystem.SelectedValue.ToString().Trim();
        SqlDataReader sdr;
        sdr = objSQL.ReturnDataReader("select DefectID,Project,Status,Detected_In_Release,System_Detected_In from Defects where Detected_In_Release like '%" + release_name + "%' and Project in(" + project_list + " )  and System_Detected_In='" + system_name + "'  order by Project");
        int count = 0;
        string projectList1 = "";

        int statusClosed = 0;
        string statusClosedList = "";
        Array statusClosedarray;

        int statusReopen = 0;
        string statusReopenList = "";
        Array statusReopenarray;

        int statusOpen = 0;
        string statusOpenList = "";
        Array statusOpenarray;

        int statusNew = 0;
        string statusNewList = "";
        Array statusNewarray;

        int statusFixed = 0;
        string statusFixedList = "";
        Array statusFixedarray;

        string projectList = "";

        if (sdr.HasRows)
        {
            while (sdr.Read())
            {
                TableCell tbcell1 = new TableCell();
                TableCell tbcell2 = new TableCell();
                TableCell tbcell3 = new TableCell();
                TableCell tbcell4 = new TableCell();
                TableRow tbRow = new TableRow();
                //project array
                if (projectList1 == "" || sdr["Project"].ToString().Trim() != projectList1)
                {
                    projectList = projectList.Trim().ToString() + sdr["Project"].ToString().Trim() + ",";
                    tbcell1.Text = sdr["Project"].ToString().Trim();
                    tbcell2.Text = "&nbsp;&nbsp;<a href=\"#\" onclick=\"javascript: open_piechart('" + sdr["Project"].ToString().Trim() + "','" + release_name + "','" + system_name + "',0);\">Defect By Priority</a>";
                    tbcell3.Text = "&nbsp;&nbsp;<a href=\"#\" onclick=\"javascript: open_piechart('" + sdr["Project"].ToString().Trim() + "','" + release_name + "','" + system_name + "',1);\">Defect By Team</a>";
                    tbcell4.Text = "&nbsp;&nbsp;<a href=\"#\" onclick=\"javascript: open_piechart('" + sdr["Project"].ToString().Trim() + "','" + release_name + "','" + system_name + "',2);\">Defect Summary</a>";
                    tbRow.Cells.Add(tbcell1);
                    tbRow.Cells.Add(tbcell2);
                    tbRow.Cells.Add(tbcell3);
                    tbRow.Cells.Add(tbcell4);
                    DynTable.Rows.Add(tbRow);
                }

                if (sdr["Project"].ToString().Trim() != projectList1 && projectList1 != "")
                {
                    statusClosedList = statusClosedList + statusClosed.ToString().Trim() + ",";
                    statusReopenList = statusReopenList + statusReopen.ToString().Trim() + ",";
                    statusOpenList = statusOpenList + statusOpen.ToString().Trim() + ",";
                    statusNewList = statusNewList + statusNew.ToString().Trim() + ",";
                    statusFixedList = statusFixedList + statusFixed.ToString().Trim() + ",";
                    statusReopen = 0;
                    statusOpen = 0;
                    statusClosed = 0;
                    statusNew = 0;
                    statusFixed = 0;
                }

                if (sdr["Status"].ToString().Trim() == "Closed")
                {
                    statusClosed = statusClosed + 1;
                }

                if (sdr["Status"].ToString().Trim() == "Reopen")
                {
                    statusReopen = statusReopen + 1;
                }

                if (sdr["Status"].ToString().Trim() == "New")
                {
                    statusNew = statusNew + 1;
                }

                if (sdr["Status"].ToString().Trim() == "Open")
                {
                    statusOpen = statusOpen + 1;
                }

                if (sdr["Status"].ToString().Trim() == "Fixed")
                {
                    statusFixed = statusFixed + 1;
                }

                projectList1 = sdr["Project"].ToString().Trim();
                count = count + 1;
            }
            divtblproj.Controls.Add(DynTable);
            statusClosedList = statusClosedList + statusClosed.ToString().Trim() + ",";
            statusReopenList = statusReopenList + statusReopen.ToString().Trim() + ",";
            statusOpenList = statusOpenList + statusOpen.ToString().Trim() + ",";
            statusNewList = statusNewList + statusNew.ToString().Trim() + ",";
            statusFixedList = statusFixedList + statusFixed.ToString().Trim() + ",";
        }
        statusClosedarray = statusClosedList.Split(new char[] { ',' });
        statusFixedarray = statusFixedList.Split(new char[] { ',' });
        statusOpenarray = statusOpenList.Split(new char[] { ',' });
        statusReopenarray = statusReopenList.Split(new char[] { ',' });
        statusNewarray = statusNewList.Split(new char[] { ',' });
        
        string strXML1=GenerateXML(project_list, statusClosedarray, statusOpenarray, statusNewarray, statusFixedarray, statusReopenarray,release_name);
        fcLiteral.Text = FusionCharts.RenderChart("FusionCharts/FCF_MSColumn3D.swf", "", strXML1, "myNext", "800", "500", false, false);
        fcLiteral.Visible = true;
    }

    public string GenerateXML(string project_list, Array statusClosedarray, Array statusOpenarray, Array statusNewarray, Array statusFixedarray, Array statusReopenarray, string release_name)
    {
        string strXML = "";
        strXML = "<graph xaxisname='Projects' yaxisname='Number of Defects' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='1' yAxisMaxValue='100' numdivlines='9' divLineColor='CCCCCC' divLineAlpha='80' decimalPrecision='0' showAlternateHGridColor='1' AlternateHGridAlpha='30' AlternateHGridColor='CCCCCC' caption='Bar Chart - " + release_name + "(" + ddSystem.SelectedValue.ToString().Trim() + ")' subcaption=''>";

        //CATEGORIES
        strXML += "<categories font='Arial' fontSize='11' fontColor='000000'>";
        foreach (string items in project_list.ToString().Split(new char[] { ',' }))
        {
            string itm = "";
            if (items.ToString().IndexOf('(') != -1)
            {
                string tmpitm = items.ToString().Remove(items.ToString().IndexOf('('), ((items.Length - 1) - items.ToString().IndexOf('(')));
                if (tmpitm.Length > 30)
                {
                    itm = tmpitm.Substring(0, 30) + "..'";
                }
                else
                {
                    itm = tmpitm;
                }
            }
            else
            {
                itm = items.ToString().Trim();
            }
            strXML += "<category name=" + itm.ToString().Trim() + " />";
        }
        strXML += "</categories>";

        //statusClosed
        strXML += "<dataset seriesname='Closed' color='8BBA00'>";
        foreach (string items in statusClosedarray)
        {
            if (items != "")
            {
                strXML += "<set value='" + items + "' />";
            }
        }
        strXML += "</dataset>";

        //statusOpen
        strXML += "<dataset seriesname='Open' color='F6BD0F'>";
        foreach (string item in statusOpenarray)
        {
            if (item != "")
            {
                strXML += "<set value='" + item + "' />";
            }
        }
        strXML += "</dataset>";

        //statusNew
        strXML += "<dataset seriesname='New' color='008ED6'>";
        foreach (string item in statusNewarray)
        {
            if (item != "")
            {
                strXML += "<set value='" + item + "' />";
            }
        }
        strXML += "</dataset>";

        //StatusFixed
        strXML += "<dataset seriesname='Fixed' color='8E468E'>";
        foreach (string item in statusFixedarray)
        {
            if (item != "")
            {
                strXML += "<set value='" + item + "' />";
            }
        }
        strXML += "</dataset>";

        //StatusReopen
        strXML += "<dataset seriesname='Reopen' color='9D080D'>";
        foreach (string item in statusReopenarray)
        {
            if (item != "")
            {
                strXML += "<set value='" + item + "' />";
            }
        }
        strXML += "</dataset>";
        strXML += "</graph>";
        return strXML;
    }
}